package fm.dongman.animefm.view.util

import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.PorterDuff
import android.graphics.PorterDuffXfermode
import android.graphics.Rect
import android.graphics.RectF


/**
 * bitmap裁剪工具
 * Created by shize on 2017/3/27.
 */

object BitmapHelper {
    /**
     * 裁剪模式
     */
    val MODE_FILLET = 0x10 // 裁剪图片大小的圆角矩形图片
    val MODE_CIRCLE = 0x30 // 裁剪圆形图片
    val MODE_FILLET_SQUARE = 0x20 // 裁剪圆角方形图片

    /**
     * 裁剪bitmap为方形圆角bitmap

     * @param bitmap       Bitmap root
     * *
     * @param filletRadius Fillet radius
     * *
     * @param scissorMode  One of [.MODE_FILLET],[.MODE_FILLET_SQUARE],
     * *                     [.MODE_CIRCLE]
     * *
     * @return bitmap
     */
    @Synchronized fun scissorBitmap(bitmap: Bitmap, filletRadius: Float, scissorMode: Int): Bitmap {
        var rectRadius = filletRadius
        val rectF: RectF
        // 根据图片的长宽获取适合的尺寸
        val length = Math.min(bitmap.width, bitmap.height)
        when (scissorMode) {
            MODE_FILLET -> rectF = RectF(0f, 0f, bitmap.width.toFloat(), bitmap.height.toFloat())
            MODE_CIRCLE -> {
                rectRadius = (length / 2).toFloat()
                rectF = RectF(0f, 0f, length.toFloat(), length.toFloat())
            }
            MODE_FILLET_SQUARE -> rectF = RectF(0f, 0f, length.toFloat(), length.toFloat())
            else -> return bitmap
        }
        return drawBitmap(bitmap, rectRadius, rectF)
    }

    /**
     * 绘制指定形状bitmap

     * @param bitmap       bitmap
     * *
     * @param filletRadius 圆角半径
     * *
     * @param rectF        基础矩形
     * *
     * @return bitmap
     */
    private fun drawBitmap(bitmap: Bitmap, filletRadius: Float, rectF: RectF): Bitmap {
        val width = bitmap.width
        val height = bitmap.height
        val rLength = rectF.right.toInt()
        val bLength = rectF.bottom.toInt()

        val src = Rect((width - rLength) / 2, height - bLength, (width + rLength) / 2, bLength)
        val dst = Rect(0, 0, rLength, bLength)

        val paint = Paint()
        paint.isAntiAlias = true // 抗锯齿
        paint.color = 0xff424242.toInt() // 颜色
        val output = Bitmap.createBitmap(rLength, bLength, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(output)
        canvas.drawARGB(0, 0, 0, 0)
        // 第一个参数为基础矩形，第二、三参数为圆角半径，最后为画笔
        canvas.drawRoundRect(rectF, filletRadius, filletRadius, paint)

        paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN) // 相交模式
        canvas.drawBitmap(bitmap, src, dst, paint)
        return output
    }

    /**
     * 获取圆角位图的方法
     * @param bitmap 需要转化成圆角的位图
     * @param pixels 圆角的度数，数值越大，圆角越大
     * @return 处理后的圆角位图
     */
    fun toRoundCornerImage(bitmap: Bitmap, pixels: Int): Bitmap {
        val output = Bitmap.createBitmap(bitmap.width, bitmap.height, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(output)
        val color = 0xff424242.toInt()
        val paint = Paint()
        val rect = Rect(0, 0, bitmap.width, bitmap.height)
        val rectF = RectF(rect)
        val roundPx = pixels.toFloat()
        // 抗锯齿
        paint.isAntiAlias = true
        canvas.drawARGB(0, 0, 0, 0)
        paint.color = color
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint)
        paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
        canvas.drawBitmap(bitmap, rect, rect, paint)
        return output
    }
}
